﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.BLL;
using PBOffice.Models;

public partial class PersonalManager_Branch : System.Web.UI.Page
{
    // List<Branch> list = new BranchManager().GetAllBranch();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
            this.btnSaveModify.Enabled = false;
        }
    }
    private void BindData()
    {
        List<Branch> list = new BranchManager().GetAllBranch();
        this.gvBranch.DataSource = list;
        gvBranch.DataBind();

    }
    protected void gvBranch_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int ide = Convert.ToInt32(gvBranch.DataKeys[e.RowIndex].Value);
        new BranchManager().DeleteBranch(ide);
        BindData();


    }

    private bool CheckEempt()
    {
        if (this.txtBrnachName.Text == "")
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('机构名称不能为空')</script>");
            return false;

        }
        if (this.txtBranchShortName.Text == "")
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('机构简称不能为空')</script>");
            return false;

        }
        return true;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (CheckEempt() == true)
        {
            Branch branch = new Branch();
            branch.BranchName = this.txtBrnachName.Text.Trim();
            branch.BranchShortName = this.txtBranchShortName.Text.Trim();
            branch.PrincipalUser = this.txtPercipualUser.Text.Trim();
            branch.ConnectTelNo = this.txtTelephone.Text.Trim();
            branch.ConnectMobileTelNo = this.txtModileTelphone.Text.Trim();
            branch.Faxes = this.txtFaxs.Text.Trim();
            new BranchManager().AddBranch(branch);
            BindData();
            this.txtBrnachName.Text = "";
            this.txtBranchShortName.Text = "";
            this.txtFaxs.Text = "";
            this.txtModileTelphone.Text = "";
            this.txtPercipualUser.Text = "";
            this.txtTelephone.Text = "";
        }

    }
    public static int id;
    protected void gvBranch_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        id = (int)gvBranch.DataKeys[e.RowIndex].Value;
        Branch branch = new BranchManager().GetOneBranch(id);
        this.txtBrnachName.Text = branch.BranchName;
        this.txtBranchShortName.Text = branch.BranchShortName;
        this.txtPercipualUser.Text = branch.PrincipalUser;
        this.txtTelephone.Text = branch.ConnectTelNo;
        this.txtModileTelphone.Text = branch.ConnectMobileTelNo;
        this.txtFaxs.Text = branch.Faxes;
        this.btnSaveModify.Enabled = true;
    }
    protected void btnSaveModify_Click(object sender, EventArgs e)
    {
        if (CheckEempt() == true)
        {
            Branch branch = new Branch();
            branch.BranchId = id;
            branch.BranchName = this.txtBrnachName.Text.Trim();
            branch.BranchShortName = this.txtBranchShortName.Text.Trim();
            branch.PrincipalUser = this.txtPercipualUser.Text.Trim();
            branch.ConnectTelNo = this.txtTelephone.Text.Trim();
            branch.ConnectMobileTelNo = this.txtModileTelphone.Text.Trim();
            branch.Faxes = this.txtFaxs.Text.Trim();
            new BranchManager().UpdateBranch(branch);
            BindData();
            this.txtBrnachName.Text = "";
            this.txtBranchShortName.Text = "";
            this.txtFaxs.Text = "";
            this.txtModileTelphone.Text = "";
            this.txtPercipualUser.Text = "";
            this.txtTelephone.Text = "";
            this.btnSaveModify.Enabled = false;
        }

    }


}